﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenExpressApp
{
    public class VisibleRegister : IVisibleRegister
    {
        private IList<IVisibleChecker> allViewVisibleChecker = new List<IVisibleChecker>();
        private Dictionary<Type, IList<IVisibleChecker>> visibleCheckerDic = new Dictionary<Type, IList<IVisibleChecker>>();

        public void VisibleCheckerRegister(Type view, IVisibleChecker visibleCheck)
        {
            IList<IVisibleChecker> visibleCheckList;

            if (!visibleCheckerDic.ContainsKey(view))
            {
                visibleCheckList = new List<IVisibleChecker>();

            }
            else
            {
                visibleCheckerDic.TryGetValue(view, out visibleCheckList);
            }
            visibleCheckList.Add(visibleCheck);

        }

        public void VisibleCheckerRegister(IVisibleChecker visibleCheck)
        {
            if (!allViewVisibleChecker.Contains<IVisibleChecker>(visibleCheck))
            {
                allViewVisibleChecker.Add(visibleCheck);
            }
        }
        public IList<IVisibleChecker> GetVisibleCheckerList(Type view)
        {
            IList<IVisibleChecker> result = new List<IVisibleChecker>();
            foreach (IVisibleChecker item in allViewVisibleChecker)   //添加公共的检验规则
                result.Add(item);

            IList<IVisibleChecker> currCheckList;
            if (visibleCheckerDic.TryGetValue(view, out currCheckList))
            {
                foreach (IVisibleChecker item in currCheckList)
                    result.Add(item);
            }

            return result;   
        }

    }
}
